from fastapi import APIRouter, Cookie, Query, status, HTTPException, Response

from typing import Annotated, Optional

from src.user_service.service.account import User_service
from src.user_service.service.account_search import Search_user_service
from src.user_service.schemas import (
    Update_form
)


account_router = APIRouter()


@account_router.get(
    "/accounts/search",
    # response_model=List[My_account],
    status_code=status.HTTP_200_OK
)
async def search_account(
    firstName: Optional[str] = None,
    lastName: Optional[str] = None,
    email: Optional[str] = None,
    from_var: Annotated[int | None, Query(alias="from")] = 0,
    size: Optional[int] = 10,
    id: Annotated[int | None, Cookie()] = None
):
    if from_var < 0 or size <= 0:
        raise HTTPException(status_code=400, detail="bad parametrs")

    if size is None:
        size = 10
    if from_var is None:
        from_var = 0

    return await Search_user_service().search_account(
        first_name=firstName,
        last_name=lastName,
        email=email,
        from_var=from_var,
        size=size,
        user_id=id
    )


@account_router.get(
    "/accounts/{accountId}",
    # response_model=My_account,
    status_code=status.HTTP_200_OK
)
async def get_my_account(
    accountId: Optional[int] = None,
    id: Annotated[int | None, Cookie()] = None
):
    if accountId is None or accountId <= 0:
        raise HTTPException(status_code=400, detail="bad id")
    return await User_service().get_account(accountId, id)


@account_router.put(
    "/accounts/{accountId}",
    # response_model=My_account,
    status_code=status.HTTP_200_OK
)
async def putch_account(
    accountId: int,
    data_to_update: Update_form,
    id: Annotated[int | None, Cookie()] = None
):
    if accountId is None or accountId <= 0:
        raise HTTPException(status_code=400, detail="bad id")

    return await User_service().putch_account(
        accountId,
        data_to_update,
        id
    )


@account_router.delete(
    "/accounts/{accountId}",
    status_code=status.HTTP_200_OK
)
async def delete_account(
    accountId: int,
    response: Response,
    id: Annotated[int | None, Cookie()] = None
):
    if accountId is None or accountId <= 0:
        raise HTTPException(status_code=400, detail="bad id")

    await User_service().delete_account(accountId, id)
    response.delete_cookie("id")
    return None
